//
//  AccountModel.m
//  JackDeve
//
//  Created by apple on 17/5/10.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "AccountModel.h"
static AccountModel * m=nil;
@implementation AccountModel
{
    NSDictionary * dic;
    
}

+(instancetype)showData{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m =[[AccountModel alloc]init];
    });
    return m;
    
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    
    if (m==nil) {
        m =[super allocWithZone:zone];
    }
    return m;
    
    
}
-(id)copy{
    return self;
}

-(id)mutableCopy{
    return self;
    
}
-(void)getDataAndBlock:(callBackBlock)block{
    
   
   
    
    
    // 写请求对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 接收的输入类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    // 取本地信息
    Data * m =[HomeUserDefault getUserDefault];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          m.token,@"token",
                          nil];
    
    [manager POST:Out parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Success:%@",responseObject);
        
      
        if (responseObject!=nil) {
             dic = (NSDictionary *)responseObject;
        }
        
        if (block && dic) {
            block(dic);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"post请求失败:%@", error);
    }];
    
    
    
    
    
}
//上传头像
-(void)getImage:(UIImage *)aa{
    
    
    //1.创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    // 取本地信息
    Data *model = [HomeUserDefault getUserDefault];
    //2.上传文件
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.token forKey:@"token"];
    
    [manager POST:AVATARPICTURE_URL parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //上传文件参数    avatar_pic
        NSData *data = UIImageJPEGRepresentation(aa, 0.2);
        [formData appendPartWithFileData:data name:@"avatar_pic" fileName:@"headImage.jpeg" mimeType:@"image/jpeg"];
        //上传多张图片
//        //上传文件参数
//        for (int i = 0; i < smallImageArray.count; i++) {
//            
//            NSData  *newData = UIImageJPEGRepresentation(smallImageArray[i],0.2);
//            
//            
//            [formData appendPartWithFileData:newData name:@"myfile" fileName:@"faBiaoImage" mimeType:@"image/jpeg"];
//        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //打印上传进度
        CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
        NSLog(@"%.2lf%%", progress);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic1 = (NSDictionary *)responseObject;
        
        // 取本地信息
        Data *model2 = [HomeUserDefault getUserDefault];
        
        if (dic1[@"data"][@"avatar"] != nil) {
            model2.avatar = dic1[@"data"][@"avatar"];
            // 存储
            [HomeUserDefault userDefaultWithModel:model2];
            if (self.block && dic1) {
                
                [SVProgressHUD dismiss];
                [SVProgressHUD showSuccessWithStatus:@"更换头像成功"];
                [SVProgressHUD dismissWithDelay:1.5];
                self.block(dic1);
            }
            else{
                [SVProgressHUD showErrorWithStatus:@"更换头像失败"];
               [SVProgressHUD dismissWithDelay:1.5];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //请求失败
        NSLog(@"请求失败：%@",error);
        
    }];
    
    
}
@end
